package com.cheung.leetcode.arr;

import  org.junit.jupiter.api.Test;

import java.util.Arrays;

/**
 * @BelongsProject : java-leetcode
 * @BelongsPackage : com.cheung.leetcode.arr
 * @Author :  cheungming
 * @CreateTime : 2024-07-25 13:04:44
 * @Description : 135 贪心算法
 * @Version : 1.0
 */
public class Code135Test {
    public int candy(int[] ratings) {
        int[] left = new int[ratings.length];
        int[] right = new int[ratings.length];
        Arrays.fill(left, 1);
        Arrays.fill(right, 1);

        for (int i = 1; i < ratings.length; i++) {
            if (ratings[i] > ratings[i - 1]) {
                left[i] = left[i - 1] + 1;
            }
        }

        for (int i = ratings.length - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1]) {
                right[i] = right[i + 1] + 1;

            }
        }

        int sum = 0;
        for (int i = 0; i < ratings.length; i++) {
            sum += Math.max(left[i], right[i]);
        }
        return sum;
    }

    @Test
    public void test1() {
        assert candy(new int[]{1, 2, 2}) == 4;
    }

    @Test
    public void test2() {
        assert candy(new int[]{1, 0, 2}) == 5;
    }

    @Test
    public void test3() {
        assert candy(new int[]{1, 3, 2, 2, 1}) == 7;
    }

    @Test
    public void test4() {
        assert candy(new int[]{1, 3, 4, 5, 2}) == 11;
    }
}
